<?php
namespace App\Http\Controllers\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\DB;
use App\Models\OrderModel;
use App\Models\OrderInfoModel;
use App\Models\GoodsModel;

class OrderController extends Controller{

    public function __construct()
    {
        $this->middleware('user_login');
    }

    /**
     * 会员购买商品
     */
    public function add_order(Request $request,OrderModel $orderModel,OrderInfoModel $orderInfoModel,GoodsModel $goodsModel){

        //var_dump($request->input('order_info'));die;
        DB::beginTransaction();
        try{
            //验证规则
            $rule = [
                'goods_id' => 'required|exists:goods,id',
                'originator_name' => 'required',
                'order_info' => 'required|array',
                'order_info.*.real_name' => 'required|distinct',
                'order_info.*.id_card' => 'required|distinct',
                //'order_info.*.passport' => 'required|distinct',

            ];

            // 验证信息
            $message = [

                'goods_id.required' => '商品ID必传',
                'goods_id.exists' => '商品ID不存在',
                'originator_name.required' => '发起人姓名必传',
                'order_info.required' => 'order_info不为空',
                'order_info.array' => 'order_info格式为数组',
                'order_info.*.real_name.required' => '登记人姓名不能为空',
                'order_info.*.real_name.distinct' => '登记人姓名不能重复',
                'order_info.*.id_card.distinct' => '登记人身份证不能重复',
                'order_info.*.id_card.required' => '登记人身份证不能为空',
                //'order_info.*.passport.required' => '登记人护照不能为空',


            ];

            $validator = Validator::make($request->all(), $rule, $message);
            if ($validator->fails()) {
                throw new \Exception($validator->errors()->first());
            }

            $goods_id = $request->input('goods_id');
            $goods = $goodsModel->where('id',$goods_id)->first();
            $order_info = $request->input('order_info');

            if(time()>=strtotime($goods->start_time)){
                return returnMsg(400,'登记超过开始时间,不能登记');
            }

            $orderModel->goods_id = $goods_id;
            $orderModel->goods_name = $goods->title;
            $orderModel->user_id = auth('user_api') -> user()->id;
            $orderModel->originator_name = $request->input('originator_name');
            $orderModel->head_img = auth('user_api') -> user()->head_img;
            $orderModel->enrolment_number = count($order_info);
            $orderModel->country_id = $goods->country_id;
            $orderModel->country = $goods->country;
            $orderModel->line_id = $goods->line_id;
            $orderModel->line = $goods->line;
            $orderModel->city_id = $goods->city_id;
            $orderModel->city = $goods->city;
            $orderModel->start_time = $goods->start_time;
            $orderModel->end_time = $goods->end_time;
            $orderModel->price = $goods->price;
            $orderModel->price_info = $goods->price_info;
            $orderModel->remarks = $request->input('remarks');
            $orderModel->pay_status = 2;

            $res= $orderModel->save();
            if(!$res){
                return returnMsg(400,'保存失败');
            }

            //插入详情
            foreach ($order_info as $k=>$v){
                $order_info[$k]['order_id'] = $orderModel->id;
            }

            $insert_order_info = DB::table('order_info')->insert($order_info);
            if(!$insert_order_info){
                return returnMsg(400,'保存失败-订单详情');
            }

            return returnMsg(0,'登记成功',$orderModel->id);

            DB::commit();

        }catch (\Exception $e){
            DB::rollBack();
            return returnMsg(400,$e -> getMessage());
        }

    }

    /**
     *我的订单列表
     */
    public function my_order(OrderModel $orderModel){

        $data = $orderModel->where('user_id',auth('user_api') -> user()->id)->paginate(15);
        return returnMsg(0,'获取成功',$data);
    }

    /**
     * 订单详情
     */
    public function order_info(Request $request,OrderModel $orderModel){

        if(empty($request->input('order_id'))){
            return returnMsg(400,'order_id参数必须');
        }

        $data = $orderModel
            ->where('id',$request->input('order_id'))
            ->where('user_id',auth('user_api') -> user()->id)
            ->with('order_info')
            ->first();

        return returnMsg(0,'获取成功',$data);

    }


}